<?php


namespace App\Models;


use Base\BaseModel;
use Lib\DB;

class MessageModel extends BaseModel
{
    protected $table = 'message';

    public function getMessgaeByUid($uid, $order = [], $limit = 20, $page = 1)
    {
        $count = $this->baseSelectCount(['to' => $uid, 'is_del' => 0]);
        $data = [];
        if ($count > 0) {
            $data = $this->baseSelectByPage(
                ["ORDER" => $order, 'to' => $uid, 'is_del' => 0],
                '*',
                $limit, $page
            );
        }
        return [$count, $data];
    }

    public function setAllMyMessageRead($uid)
    {
        return $this->baseUpdate([
            'to' => $uid
        ], [
            'is_read' => 1
        ]);
    }

    public function insertMessage($data)
    {
        return $this->baseInsert($data);
    }

    public function getMyUnreadCount($uid): int
    {
        return $this->baseSelectCount(['to' => $uid, 'is_read' => 0, 'is_del' => 0]) ?: 0;
    }
}
